package simulador.entidade;
import simulador.comum.Definicoes;
import eduni.simjava.Sim_entity;
import eduni.simjava.Sim_event;
import eduni.simjava.Sim_port;
import eduni.simjava.Sim_stat;
import eduni.simjava.Sim_system;
import eduni.simjava.distributions.Sim_negexp_obj;

/**
 * 
 * @author Gustavo Farias, Erick Moreno, Vinicius Marques, Magno Jefferson.
 *
 */
public class ServicoAjuste extends Sim_entity {
	
	private static final String DELAY_SERVER = "DelayServer";
	
	private static final double PROB_AJUSTE = 0.1;
	
	private Sim_port in;

	private Sim_negexp_obj delay;

	private Sim_stat stat; 

	public ServicoAjuste(String name, double mean) {
		super(name);
		
		in = new Sim_port(Definicoes.PORTA_ENTRADA_AJUSTE); 
		add_port(in);
		
		delay = new Sim_negexp_obj(DELAY_SERVER, mean);
		add_generator(delay);
		
		stat = new Sim_stat();
		stat.add_measure(Sim_stat.UTILISATION);
		stat.add_measure(Sim_stat.SERVICE_TIME);
		stat.add_measure(Sim_stat.INTERVAL_BASED);
		stat.add_measure(Sim_stat.WAITING_TIME);
		set_stat(stat);
	}

	public void body() {
		while (Sim_system.running()) {
			Sim_event e = new Sim_event();
			sim_get_next(e);
			
			double numeroAleatorio = Math.random(); // gerar numero entre 0 e 1.
			double tempoProcessamento = delay.sample(); // tempo de processamento gerado aleatoriamente.
			
			if(numeroAleatorio <= PROB_AJUSTE){
				sim_schedule(in, 0.0, Definicoes.AJUSTE); // manda pra ServicoAjuste
			}
			
			sim_process(tempoProcessamento); // processa o ajuste de matricula do aluno.
			sim_completed(e); // encerra o processamento.
			
		}
	}
}